// File for Saia Table 4/5 simulation.

clear all
set mem 1g
set maxvar 32000
set matsize 11000
set more off
local filefolder = "$path_pch\Data\raw\Voting"
local seatingfolder = "$path_pch\Data\intermediate\Seating"
local cosponfolder = "$path_pch\Data\analysis\Co-Sponsorship"
local votingfolder = "$path_pch\Data\analysis\Voting"
local votingintfolder = "$path_pch\Data\intermediate\Voting"
local saiaintfolder = "$path_pch/Data/intermediate/Saia"
local votingrawfolder = "$path_pch\Data\raw\Voting"

local sim = 100


**************************
/* Pre-iteration prep */
**************************

***
// Merge vote - party leader info, save for future use (five files: original, ULFB).
/* MP_id session_id vote_id vote chairman_id chairman_vote */

timer clear
timer on 1

// MP info...
use "`seatingfolder'\seating_MP_frontback.dta", clear
keep MP_name MP_id session_id party_id
tempfile MP
save `MP'

// first get newly collected session_id data
cd "`filefolder'"
import delimited using vote_info.csv, clear
ren v1 vote_id
ren v2 session_id
ren v3 session_id_alt
ren v4 bill_id
ren v5 datetime
assert session_id == session_id_alt
drop session_id_alt
drop if mi(session_id)
tempfile session
save `session'

// now get voting info of each MP.
cd "`filefolder'"
import delimited using vote_ind.csv, clear encoding("utf-8")
ren vote vote_raw
gen vote = "absent" if inlist(vote_raw,"fjarverandi","fjarvist")
replace vote = "abstain" if vote_raw == "greiðir ekki atkvæði"
replace vote = "no" if vote_raw == "nei"
replace vote = "yes" if vote_raw == "já"
ren name MP_name
duplicates drop //vote_id == 40426 collected twice somehow (so 63 observations should be dropped)

// Some manual fixes for abbreviation
replace MP_name = "Guðni Ágústsson" if MP_name == "GÁ"
replace MP_name = "Sighvatur Björgvinsson" if MP_name == "SighB"
replace MP_name = "Ísólfur Gylfi Pálmason" if MP_name == "ÍGP"

// merge session info
merge n:1 vote_id using `session', nogen

// merge MP_info
merge n:1 MP_name session_id using `MP', keep(3) nogen

// Get Saia's party leader vote data
preserve
	use "`votingrawfolder'/voting_procedure_with_chairman_vote_and_senior_MP_saia", clear
	drop _merge_chairman
	ren session session_id 
	*keep if session_id<=145 & (!inlist(session_id,119,124,129,134,137,142))
	ren voting_procedure_id vote_id
	ren MP_party party
	g party_id=.
	
	replace party_id=2 if party=="Alþýðubandalag"
	replace party_id=1 if party=="Alþýðuflokkur"
	replace party_id=15 if party=="Björt framtíð"
	replace party_id=13 if party=="Borgarahreyfingin"
	replace party_id=3 if party=="Framsóknarflokkur"
	replace party_id=11 if party=="Frjálslyndi flokkurinn"
	replace party_id=14 if party=="Hreyfingin"
	replace party_id=19 if party=="Píratar"	
	replace party_id=9 if party=="Samfylkingin"
	replace party_id=5 if party=="Samtök um kvennalista"
	replace party_id=4 if party=="Sjálfstæðisflokkur"
	replace party_id=6 if party=="Utan þingflokka"
	replace party_id=12 if party=="Vinstri hreyfingin - grænt framboð"
	replace party_id=16 if party=="Viðreisn"
	replace party_id=8 if party=="Þingflokkur jafnaðarmanna"
	replace party_id=10 if party=="Þingflokkur óháðra"
	replace party_id=7 if party=="Þjóðvaki - hreyfing fólksins"
	
	ren party party_saia
	tempfile leaders
	save `leaders'
restore

// merge on party leader's vote
merge m:1 session_id vote_id party_id using `leaders', keep(1 3) // some vote_id only in our data, some only in his
replace chairman_vote = "abstain" if chairman_vote=="abstained"

// list all party leaders
forv k = 1/19 {
	gen temp = chairman_id if party_id == `k'
	bys vote_id: egen party`k'_chairman_id = max(temp)
	drop temp
}

keep MP_id session_id vote_id vote chairman_id chairman_vote party*_chairman_id
save "`saiaintfolder'/saia_simul_vote.dta", replace

foreach s in U L F B {
preserve
	drop session_id party*_chairman_id
	foreach var of varlist MP_id vote chairman_id chairman_vote {
		ren `var' `s'_`var'
	}
	save "`saiaintfolder'/saia_simul_vote_`s'.dta", replace
restore
}

timer off 1
timer list // this step took 32 seconds
